
// Javascript for the comment class
// -----------------------------------------------------------------------------

var gpuCommentSystem = new Object();
gpuCommentSystem.currentlyEditing = 0;
gpuCommentSystem.process = function () {
  var comment = escape(document.forms['commentform'].comment.value);
  var type = document.forms['commentform'].type.value;
  var user_id = document.forms['commentform'].user_id.value;
  var user_name = escape(document.forms['commentform'].user_name.value);
  var item_id = document.forms['commentform'].item_id.value;
  var url = 'process_comment.php?action=add&comment=' + comment + '&item_id=' + item_id + '&type=' + type + '&user_id=' + user_id + '&user_name=' + user_name;
  var http = getHTTPObject();
  http.open("GET", url, true);
  http.onreadystatechange = Delegate.create(http, gpuCommentSystem.handleAddResponse);
  http.send(null);
}
gpuCommentSystem.handleAddResponse = function () {
  if (this.readyState == 4) {
    if (this.status == 200) {
      if (!this.responseXML || !this.responseXML.documentElement) {
        alert(this.responseText);
        return;
      }
      var insertId = this.responseXML.documentElement.getAttribute('id');
      var newcomment = '';
      var d = new Date();
      var strtime = d.toLocaleString();
      newcomment += '<div id="individual-comment-' + insertId + '">'
      newcomment += '<div class="comments-head">';
      newcomment += strtime;
      newcomment += '</div>';
      newcomment += '<div class="comments-name" style="float: left; width: 20%;">';
      if (document.forms['commentform'].user_id.value != '') newcomment += "<a href=\"http://www.gpureview.com/forums/member.php?userid=" + document.forms['commentform'].user_id.value + "\">\n";
      newcomment += document.forms['commentform'].user_name.value;
      if (document.forms['commentform'].user_id.value != '') newcomment += "</a>";
      newcomment += "<br><span class=\"small\">" + document.forms['commentform'].user_title.value + "</span>\n";

      if (document.forms['commentform'].user_id.value != '') {
        var editlink = 'javascript:gpuCommentSystem.editComment(' + insertId + ');';
        var deletelink = 'javascript:gpuCommentSystem.deleteComment(' + insertId + ');';
        newcomment += '<div style="font-size: 10px; font-weight: normal;">[<a href="' + editlink + '">Edit</a> | <a href="' + deletelink + '">Delete</a>]</div>';
      }

      newcomment += "</div>";

      var comment = document.forms['commentform'].comment.value;
      comment = comment.replace(/<\S[^>]*>/g,'');
      comment = comment.replace(/\n/g,'<br/>');


      newcomment += '<div class="comments-body" id="individual-comment-body-' + insertId + '" style="float: left; width: 70%;">' + comment + '</div>';
      newcomment += "<div style=\"clear: both;\"></div>";
      document.getElementById('comments_container').innerHTML += newcomment;
      if (document.getElementById('no_comments')) document.getElementById('no_comments').parentNode.removeChild(document.getElementById('no_comments'));
      document.forms['commentform'].comment.value = '';
    } else {
      alert('An internal error occurred: ' + this.status);
    }
  }
}
gpuCommentSystem.deleteComment = function (id) {
  if (!confirm('Are you sure you want to delete this comment?')) return;
  var url = 'process_comment.php?action=delete&id=' + id;
  var http = getHTTPObject();
  http.open("GET", url, true);
  http.onreadystatechange = Delegate.create(http, gpuCommentSystem.handleDeleteResponse, id);
  http.send(null);
}
gpuCommentSystem.handleDeleteResponse = function (obj, id) {
  if ((this.readyState == 4) && (this.responseText.length == 0)) {
    var commentBox = document.getElementById('individual-comment-' + id);
    commentBox.parentNode.removeChild(commentBox);
  } else if (this.readyState == 4) {
    alert(this.responseText);
  }
}
gpuCommentSystem.editComment = function (id) {
  if (gpuCommentSystem.currentlyEditing != 0) gpuCommentSystem.cancelEdit(gpuCommentSystem.currentlyEditing);
  gpuCommentSystem.currentlyEditing = id;
  var commentBody = document.getElementById('individual-comment-body-' + id);
  var commentText = commentBody.innerHTML.replace(/\n/g,'').replace(/<br[^>]*>/g,'\n');
  commentBody.innerHTML = '<textarea id="individual-comment-body-edit-' + id + '" style=\"width: 100%; font-size: 11px; font-family: arial; height: 100px;\">' + commentText + '</textarea>';
  commentBody.innerHTML += '<input type="button" value="Submit" onclick="gpuCommentSystem.submitEdit(' + id + ')" style="font-size: 11px;" />';
  commentBody.innerHTML += '<input type="button" value="Cancel" onclick="gpuCommentSystem.cancelEdit(' + id + ')" style="font-size: 11px;" />';
}
gpuCommentSystem.submitEdit = function (id) {
  var comment = escape(document.getElementById('individual-comment-body-edit-' + id).value);
  var url = 'process_comment.php?action=edit&id=' + id + '&comment=' + comment;
  var http = getHTTPObject();
  http.open("GET", url, true);
  http.onreadystatechange = Delegate.create(http, gpuCommentSystem.handleEditResponse, id);
  http.send(null);
}
gpuCommentSystem.handleEditResponse = function (obj, id) {
  if ((this.readyState == 4) && (this.responseText.length == 0)) {
    var commentBody = document.getElementById('individual-comment-body-' + id);
    commentBody.innerHTML = document.getElementById('individual-comment-body-edit-' + id).value.replace(/\n/g,'<br/>\n');
    gpuCommentSystem.currentlyEditing = 0;
  } else if (this.readyState == 4) {
    alert(this.responseText);
  }
}

gpuCommentSystem.cancelEdit = function (id) {
  var commentBody = document.getElementById('individual-comment-body-' + id);
  commentBody.innerHTML = document.getElementById('individual-comment-body-edit-' + id).innerHTML.replace(/\n/g,'<br/>\n');
  gpuCommentSystem.currentlyEditing = 0;
}

// Javascript for the info_block class
// -----------------------------------------------------------------------------

var gpuInfoBlock = new Object();
gpuInfoBlock.minimize = function (id) {
  document.getElementById(id).style.height = '0px';
}
gpuInfoBlock.restore = function (id) {
  document.getElementById(id).style.height = '100px';
}
gpuInfoBlock.maximize = function (id) {
  document.getElementById(id).style.height = '';
}

// Javascript for the image_viewer class
// -----------------------------------------------------------------------------

var gpuImageViewerSystem = new Object();
var g = gpuImageViewerSystem;
g.load = function () {
  var elements = document.getElementsByTagName('div');
  for (var i in elements) {
    if (elements[i].className == 'gpu-image-viewer') {
      var temp = new gpuImageViewerSystem.viewer(elements[i]);
    }
  }
}

if (window.addEventListener) window.addEventListener('load',gpuImageViewerSystem.load,false);
else if (window.attachEvent) window.attachEvent('onload',gpuImageViewerSystem.load);

g.viewer = function(element) {
  this.element = element;
  this.index = 0;
  this.multi = false;
  this.element.viewer = this;
  this.element.onmouseover = function () {
    if (this.viewer.multi) {
      if (document.all) this.viewer.toolbar.style.filter = 'alpha(opacity=100)';
      else this.viewer.toolbar.style.opacity = '1.0';
    }
  }
  this.element.onmouseout = function () {
    if (this.viewer.multi) {
      if (document.all) this.viewer.toolbar.style.filter = 'alpha(opacity=25)';
      else this.viewer.toolbar.style.opacity = '0.25';
    }
  }
  this.smallImages = new Array();
  this.bigImages = new Array();

  // Look for images and then construct the toolbar to switch between them
  this.getImages();
  if (this.multi) {
    this.makeToolbar();
  }
}
var v = gpuImageViewerSystem.viewer.prototype;
v.getImages = function () {
  var images = this.element.getElementsByTagName('img');
  for (var i in images) {
    var image = images[i];
    if (image.className == 'smallimage') {
      if (this.smallImages.length == 0) this.smallImage = image;
      this.smallImages.push(image.src);
    } else if (image.className == 'bigimage') {
      if (this.bigImages.length == 0) this.bigImage = image;
      this.bigImages.push(image.src);
    }
  }
  this.smallImage.viewer = this;
  this.bigImage.viewer = this;
  this.smallImage.onclick = function () {
    this.viewer.zoom();
  }
  this.bigImage.onclick = function () {
    this.viewer.zoom();
  }
  if (this.bigImages.length > 1) this.multi = true;
}
v.makeToolbar = function () {
  this.toolbar = document.createElement('div');
  this.toolbar.className = 'gpu-viewer-toolbar';

  var temp = document.createElement('img');
  temp.src = 'http://www.gpureview.com/images/imageviewer-previous.gif';
  temp.title = 'Previous Image';
  temp.viewer = this;
  temp.onclick = function () {
    this.viewer.previous();
  }
  temp.style.cursor = 'pointer';
  this.toolbar.appendChild(temp);

  var temp = document.createElement('img');
  temp.src = 'http://www.gpureview.com/images/imageviewer-next.gif';
  temp.title = 'Next Image';
  temp.viewer = this;
  temp.onclick = function () {
    this.viewer.next();
  }
  temp.style.cursor = 'pointer';
  this.toolbar.appendChild(temp);

  // Set the initial opacity
  if (document.all) this.toolbar.style.filter = 'alpha(opacity=25)';
  else this.toolbar.style.opacity = '0.25';

  // Add the toolbar to the div before the image so it sits at the top
  this.element.insertBefore(this.toolbar,this.element.firstChild);
}
v.zoom = function () {
  if (this.bigImage.style.display == 'block') {
    this.bigImage.style.display = 'none';
  } else {
    this.bigImage.style.display = 'block';
  }
}
v.next = function () {
  this.index++;
  if (this.index >= this.smallImages.length) this.index = 0;
  this.bigImage.src = this.bigImages[this.index];
  this.smallImage.src = this.smallImages[this.index];
}
v.previous = function () {
  this.index--;
  if (this.index < 0) this.index = this.smallImages.length - 1;
  this.bigImage.src = this.bigImages[this.index];
  this.smallImage.src = this.smallImages[this.index];
}

// Javascript for the star_rating class
// -----------------------------------------------------------------------------

var gpuRatingSystem = new Object();
var g = gpuRatingSystem;
g.load = function () {
  var elements = document.getElementsByTagName('div');
  for (var i in elements) {
    if (elements[i].className == 'gpu-rating-container-interactive') {
      var temp = new gpuRatingSystem.rating(elements[i]);
    }
  }
}

if (window.addEventListener) window.addEventListener('load',gpuRatingSystem.load,false);
else if (window.attachEvent) window.attachEvent('onload',gpuRatingSystem.load);

g.rating = function (element) {
  var pieces = element.id.split('-');

  this.type = pieces[1];
  this.category = pieces[2];
  this.id = pieces[3];
  this.http = new getHTTPObject();

  var elements = element.getElementsByTagName('div');
  for (var i in elements) {
    if (elements[i].className == 'gpu-rating-score') this.scoreDiv = elements[i];
    else if (elements[i].className == 'gpu-rating-dark') this.darkDiv = elements[i];
    else if (elements[i].className == 'gpu-rating-overlay') this.overlayDiv = elements[i];
  }
  if (!this.scoreDiv || !this.darkDiv || !this.overlayDiv) return;

  this.originalWidth = this.darkDiv.style.width;

  this.overlayDiv.style.cursor = 'pointer';
  this.overlayDiv.ratingObj = this;
  this.overlayDiv.onmousemove = function (event) {
    var e = event;
    if (!e) e = window.event;
    if (!e.pageX) e.pageX = e.clientX;
    if (!e.pageY) e.pageY = e.clientY;

    var temp = e.pageX - findPosX(this.ratingObj.darkDiv);
    temp = Math.round(temp / 7.2);
    temp = temp * 7.2;
    this.ratingObj.darkDiv.style.width = temp + 'px';
  }
  this.overlayDiv.onmouseout = function (event) {
    this.ratingObj.darkDiv.style.width = this.ratingObj.originalWidth;
  }
  this.overlayDiv.onclick = function (event) {
    var rating = parseInt(this.ratingObj.darkDiv.style.width) / 7.2;
    rating = Math.round(rating);
    var resultUrl = 'process_rating.php?item_type=' + this.ratingObj.type +
                                      '&item_id=' + this.ratingObj.id +
                                      '&rating=' + rating +
                                      '&category=' + this.ratingObj.category;
    this.ratingObj.http.open('GET', resultUrl, true);
    this.ratingObj.http.onreadystatechange = Delegate.create(this.ratingObj, this.ratingObj.parseResult);
    this.ratingObj.http.send(null);
  }
  this.parseResult = function() {
    if (this.http.readyState == 4) {
      var pieces = this.http.responseText.split(':');
      var votes = pieces[0];
      var rating = pieces[1];
      var id = pieces[2];
      var category = pieces[3];

      var temp = Math.round(rating * 7.2);
      this.darkDiv.style.width = temp + 'px';
      this.scoreDiv.innerHTML = '<strong>' + rating + '</strong> (' + votes + ' votes)';
      this.overlayDiv.onmousemove = null;
      this.overlayDiv.onmouseout = null;
      this.overlayDiv.onclick = null;
      this.overlayDiv.style.cursor = 'default';
      this.overlayDiv.title = 'Thanks For Voting!';
    }
  }

}
